//
//  AFFRichTextNumber.m
//  AnyfishApp
//
//  Created by Bob Lee on 14-6-3.
//  Copyright (c) 2014年 Anyfish. All rights reserved.
//

#import "AFFRichTextNumber.h"

@implementation AFFRichTextNumber
- (id)init
{
    self = [super init];
    if (self) {
        self.textType = ERichTextType_Number;
        self.isResponseTouch = YES;
        self.textColor = kSetting.color_2984e9;
    }
    return self;
}

//-- 替换基础文本
- (void)replaceTextWithAttributedString:(NSMutableAttributedString*) attributedString
{
    if(self.textColor)
        [attributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)self.textColor.CGColor range:self.range];
    
    if(self.originalFont)
        [attributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)self.originalFont range:self.range];
    
    NSDictionary *underlineAttribute = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};
    [attributedString addAttributes:underlineAttribute range:self.range];
    [super replaceTextWithAttributedString:attributedString];
}

//-- 绘制内容
- (BOOL)drawRunWithRect:(CGRect)rect
{
    return NO;
}

//-- 解析文本内容
+ (NSString *)analyzeText:(NSString *)string runsArray:(NSMutableArray **)runArray
{
    NSError *error;
    NSString *regulaStr = /*@"(^(\\+?86 ?)?((13[0-9]|15[0-35-9]|177|18[0-9])\\-?(\\d{4}\\-?\\d{4})))|(^(0[1-9][0-9]{1,2}\\-?)?([2-9][0-9]{6,7})(\\-[0-9]{1,4})?$)|(^400\\-?\\d{3}\\-?\\d{4})|(^400\\-?\\d{4}\\-?\\d{3})";*/
    @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$|^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$|^1(3[0-2]|5[256]|8[56])\\d{8}$|^1((33|53|8[09])[0-9]|349)\\d{7}$|((\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    NSArray *arrayOfAllMatches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
    
    for (NSTextCheckingResult *match in arrayOfAllMatches)
    {
        NSString* substringForMatch = [string substringWithRange:match.range];
        AFFRichTextNumber *urlRun = [[AFFRichTextNumber alloc] init];
        urlRun.range = match.range;
        urlRun.originalText = substringForMatch;
        [*runArray addObject:urlRun];
    }
    
    return [string copy];
}

+ (BOOL)isValidNumber:(NSString *)str{
    NSError *error;
    NSString *regulaStr = @"(^(\\+?86 ?)?((13[0-9]|15[0-35-9]|177|18[0-9])\\-?(\\d{4}\\-?\\d{4})))|(^(0[1-9][0-9]{1,2}\\-?)?([2-9][0-9]{6,7})(\\-[0-9]{1,4})?$)|(^400\\-?\\d{3}\\-?\\d{4})|(^400\\-?\\d{4}\\-?\\d{3})";
    //@"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$|^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$|^1(3[0-2]|5[256]|8[56])\\d{8}$|^1((33|53|8[09])[0-9]|349)\\d{7}$|((\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    NSArray *arrayOfAllMatches = [regex matchesInString:str options:0 range:NSMakeRange(0, [str length])];
    
    if(!arrayOfAllMatches||arrayOfAllMatches.count<=0)
        return NO;
    
    NSTextCheckingResult *match = arrayOfAllMatches[0];
    return  match.range.length == [str length];
}
@end
